package com.ly.heart.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ly.common.core.domain.AjaxResult;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.mapper.KingBaseRateMapper;
import com.ly.heart.service.IKingBaseRateService;
import org.springframework.stereotype.Service;
import com.ly.heart.domain.KingBaseRateEntity;

import javax.annotation.Resource;

/**
 * 金蝶-财务模块-汇率体系Service业务层处理
 *
 * @author wutao
 * @date 2024-06-22
 */
@Service
public class KingBaseRateServiceImpl extends ServiceImpl<KingBaseRateMapper, KingBaseRateEntity> implements IKingBaseRateService {

    @Resource
    private KingBaseRateMapper kingBaseRateEntityMapper;

    /**
     * 查询金蝶-财务模块-汇率体系
     *
     * @param id 金蝶-财务模块-汇率体系ID
     * @return 金蝶-财务模块-汇率体系
     */
    @Override
    public KingBaseRateEntity selectKingBaseRateEntityById ( String id ) {
        return kingBaseRateEntityMapper.selectKingBaseRateEntityById ( id );
    }

    /**
     * 查询金蝶-财务模块-汇率体系列表
     *
     * @param kingBaseRateEntity 金蝶-财务模块-汇率体系
     * @return 金蝶-财务模块-汇率体系
     */
    @Override
    public List<KingBaseRateEntity> selectKingBaseRateEntityList ( KingBaseRateEntity kingBaseRateEntity ) {
        return kingBaseRateEntityMapper.selectKingBaseRateEntityList ( kingBaseRateEntity );
    }

    /**
     * 新增金蝶-财务模块-汇率体系
     *
     * @param kingBaseRateEntity 金蝶-财务模块-汇率体系
     * @return 结果
     */
    @Override
    public int insertKingBaseRateEntity ( KingBaseRateEntity kingBaseRateEntity ) {
        kingBaseRateEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
//        kingBaseRateEntity.setDeptId ( CurrentUserUtil.getCompanyId () );
//        kingBaseRateEntity.setCreateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingBaseRateEntity.setCreateTime ( DateUtils.getNowDate () );
        return kingBaseRateEntityMapper.insertKingBaseRateEntity ( kingBaseRateEntity );
    }

    /**
     * 修改金蝶-财务模块-汇率体系
     *
     * @param kingBaseRateEntity 金蝶-财务模块-汇率体系
     * @return 结果
     */
    @Override
    public int updateKingBaseRateEntity ( KingBaseRateEntity kingBaseRateEntity ) {
        kingBaseRateEntity.setUpdateTime ( DateUtils.getNowDate () );
        return kingBaseRateEntityMapper.updateKingBaseRateEntity ( kingBaseRateEntity );
    }

    /**
     * 批量删除金蝶-财务模块-汇率体系
     *
     * @param ids 需要删除的金蝶-财务模块-汇率体系ID
     * @return 结果
     */
    @Override
    public AjaxResult deleteKingBaseRateEntityByIds ( String[] ids ) {
        List<KingBaseRateEntity> list = this.list ( new LambdaQueryWrapper<KingBaseRateEntity> ().in ( KingBaseRateEntity :: getId, ids ) );
        List<String> idList = list.stream ().filter ( rate -> rate.getIsDel () == 1 ).map ( KingBaseRateEntity :: getId ).collect ( Collectors.toList () );
        StringBuilder stringBuilder = new StringBuilder ( "【" );
        list.forEach ( rate -> {
            if ( rate.getIsDel () == 2 ) {
                stringBuilder.append ( rate.getDirectRate () );
            }
        } );
        stringBuilder.append ( "】" );
        boolean update = this.update ( new LambdaUpdateWrapper<KingBaseRateEntity> ().set ( KingBaseRateEntity :: getUpdateBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).set ( KingBaseRateEntity :: getUpdateTime, DateUtils.getNowDate () ).in ( KingBaseRateEntity :: getId, idList ) );
        if ( ! update ) {
            throw new CustomException ( "删除汇率体系失败" );
        }
        String str = "删除汇率体系成功";
        if ( list.size () > 0 ) {
            str.concat ( "，其中" ).concat ( stringBuilder.toString () ).concat ( "被引用，不可删除" );
        }
        return AjaxResult.success ( str );
    }

    /**
     * 删除金蝶-财务模块-汇率体系信息
     *
     * @param id 金蝶-财务模块-汇率体系ID
     * @return 结果
     */
    @Override
    public int deleteKingBaseRateEntityById ( String id ) {
        return kingBaseRateEntityMapper.deleteKingBaseRateEntityById ( id );
    }
}
